<?php
namespace app\api\controller;
use app\common\controller\ApiBase;
use app\common\library\ErrorType;
use app\common\library\Sign;
use think\facade\Config;

/**
 * @title 个人账户类
 * @description 个人账户类
 * @package app\api\controller
 */
class Account extends ApiBase
{
    /**
     * @var 用户登陆的id
     */
    protected $uid;
    /**
     * @var array 前置操作，这里是检查登录
     */
    protected $beforeActionList = [
        'checkAuth'
    ];

    /**
     * @title 检查登录
     * @description 检查登录
     * @author Admin ldy
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/19  9:36
     */
    protected function checkAuth(){
        /// 如需登录，调用此方法鉴权
        /// 注解1：如果已经登录，则会获取到uid;否则会直接打印错误信息，不执行下面的代码
        /// 注解2：该方法走的是缓存，不通过数据库检查
        /// 注解3：如果没有token信息，需要调登录接口，重新获取token，并保存，token有效期暂定为30天
        /// 注解4：该类所有方法需要登录鉴权，故预先加载
        $this->uid = $this->getUidByCheck();
    }

    /**
     * @title 获取个人数据
     * @description 获取个人数据
     * @author Admin ldy
     * @url /api/account/getPersonDetail
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/26  8:24
     */
    public function getPersonDetail(){
        $user =  \app\common\model\User::getById($this->uid);
        if ($user){
            $this->success('获取数据成功！',$user);
        }else{
            $this->err(ErrorType::GET_DATA_FAILED);
        }
    }

    /**
     * @title 签到
     * @description 签到
     * @author Admin ldy
     * @url /api/account/Sign
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/9/28  14:27
     */
    public function Sign(){
        //用户uid
        $uid = $this->uid;
        $re =Sign::recordToday($uid);
        if ($re){
            $this->success('签到成功！','',200);
        }else{
            $this->err(ErrorType::ACTION_FAILED);
        }
    }

    /**
     * @title 根据健值修改用户数据
     * @description 根据健值修改用户数据,可以操作的数据有：<br>wx_nickname\nickname\sex\birthday\introduction\mobile\introduction_url\er_code\head_url\desc\invite_code
     * @description <br>sex 性别(1是男，2是女，3是其他)
     * @description <br>birthday 生日(格式是1999-01-02)
     * @description <br>mobile 手机号(13位手机号)
     * @description <br>introduction_url 个人主页(url链接)
     * @description <br>er_code 二维码(url链接)
     * @description <br>invite_code 邀请码
     * @description <br>wx_nickname 微信的昵称
     * @author Admin ldy
     * @url /api/account/updateKeyByValue
     * @param name:token  type:string require:1 default:1 desc:身份验证
     * @param name:key  type:string require:1 default:1 desc:健值如：nickname\sex\birthday\introduction\mobile\introduction_url\er_code\head_url\desc\invite_code
     * @param name:value  type:string require:1 default:1 desc:相应健值的数据值
     * @time 2018/9/28  14:27
     */
    public function updateKeyByValue(){
        //用户uid
        $uid = $this->uid;
        $key = $this->request->param('key');
        $value_post = $this->request->param('value');
        $this->checkEmptyParams($key);
        $canDealArray = array('wx_nickname','nickname','sex','birthday','introduction','mobile','introduction_url','er_code','head_url','desc','invite_code');
        $isin = in_array($key,$canDealArray);
        if (!$isin){
            $this->err(ErrorType::DATA_EXCEPTION);
        }
        $checkdata = [
            $key => $value_post,
        ];
        $validate = new \app\common\validate\User;
        if (!$validate->scene('update_'.$key)->check($checkdata)) {
            $this->error($validate->getError());
        }
        $user =  \app\common\model\User::getById($uid);
        if ($user){
            $data[$key] = $value_post;
            $re = $user->save($data);
            if ($re){
                //发送经验值
                sendExpTo($uid,66);
                //发送积分奖励
                sendScoreTo($uid,10);
                //发送站内信
                $title='更新【'.$key.'】成功！';
                $content = '恭喜您！'.$title.' 请注意查收您的积分等奖励^_^ ';
                sendMsgToUser($uid,$title,$content);

                $this->success('更新数据成功！',$user,200);
            }else{
                $this->err(ErrorType::ACTION_FAILED);
            }
        }else{
            $this->err(ErrorType::GET_DATA_FAILED);
        }
    }

    public function setUserDatas(){
     //等待开发
    }



    /**
     * @title 测试
     * @description 测试
     * @author Admin ldy
     * @url /api/account/test
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/10/15  13:50
     */
    public function test(){
//        $das1 = Sign::checkUserActive(14);
//        $this->success('成功',$das1,200);

//        $re = \app\common\library\YuActivity::sendEmailToUserInBackGround('272604545@qq.com','注册成功！','亲爱的136***0382用户，你好！恭喜您注册收藏家成功。请牢记的您的账户和密码。');
//        $re = \app\common\library\YuActivity::sendInnerMsgToUserInBackGround(33,'测试消息555','测试消息的内容哈！！');
//        $re = \app\common\library\YuActivity::sendExperienceToUserInBackGround(33,50);
////        $re = \app\common\library\YuActivity::sendScoreToUserInBackGround(33,20);
//        if ($re->getStatus()){
//            $this->success('成功！',$re->getData(),200);
//        }else{
//            $this->err($re->getMsg());
//        }

        //测试发送一条站内信
//        $re = \app\common\library\YuMsg::sendMsgToUserByUserId($this->uid,'测试消息1','这是一条测试消息1');
//        if ($re->getStatus()){
//            $this->success('成功！',$re->getData(),200);
//        }else{
//            $this->err($re->getMsg());
//        }

//        $re = \app\common\library\YuAccount::addExperience($this->uid,'10');
//        if ($re->getStatus()){
//            $this->success('成功！',$re->getData(),200);
//        }else{
//            $this->err($re->getMsg());
//        }

        $re = \app\common\library\YuUnDeleteType::getUserUnDeleteType(42);
        if ($re->getStatus()){
            $this->success('成功！',$re->getData(),200);
        }else{
            $this->err($re->getMsg());
        }
    }

    /**
     * @title 获取用户的总签到数
     * @description 获取用户的总签到数
     * @author Admin ldy
     * @url /api/account/getSignData
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/10/22  12:15
     */
    public function getSignData(){
        $uid = $this->uid;
        $re = \app\common\library\Sign::countSign($uid);
        if ($re){
            $this->success('',$re,200);
        }else{
            $this->err(ErrorType::ACTION_FAILED);
        }
    }

    /**
     * @title 获取用户的转码基数
     * @description 获取用户的转码基数
     * @author Admin ldy
     * @url /api/account/getBaseNum
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/10/24  15:09
     */
    public function getBaseNum(){
        $this->success('获取成功',Config::get('scj.base_num'));
    }

    /**
     * @title 获取用户的统计数据
     * @description 获取用户的统计数据
     * @author Admin ldy
     * @url /api/account/getCountData
     * @param name:token  type:string require:1 default:1 desc:
     * @time 2018/10/24  15:09
     */
    public function getCountData(){
        $uid = $this->uid;
        $result=array(
            'msg_un_read'=>'',
            'url_num'=>'',
            'type_num'=>'',
            'score'=>'',
            'experience'=>'',
            'favour'=>'',//被赞同数
        );
        //未读消息数
        $unread = \app\common\library\YuMsg::getMsgCountUnReadByUserId($uid);
        if ($unread->getStatus()){
            $result['msg_un_read'] = $unread->getData();
        }
        //收藏的url数
        $perUrl = \app\common\library\YuDa::getPersonUrlCountByUerId($uid);
        if ($perUrl->getStatus()){
            $result['url_num'] = $perUrl->getData();
        }
        //文件夹总数
        $perType = \app\common\library\YuDa::getPersonTypeCountByUerId($uid);
        if ($perType->getStatus()){
            $result['type_num'] = $perType->getData();
        }
        //积分
        //经验值
        $where = array(
            'id'=>$uid
        );
        $user = \app\common\model\User::where($where)->field('experience,score')->find();
        if ($user){
            $result['score'] = $user['score'];
            $result['experience'] = $user['experience'];
        }
        //被点赞总数
        $this->success('获取成功!',$result);
    }


}
